package bio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 异步线程优化
 * @Author: 张广焱
 * @Date: 2024-05-01 上午10:40
 */
public class BioServer2 {
    private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 3, TimeUnit.MINUTES, new ArrayBlockingQueue<>(100));

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket();
        serverSocket.bind(new InetSocketAddress(9090));
        while (true) {
            try {
                Socket socket = serverSocket.accept();
                threadPoolExecutor.execute(()->{
                    try {
                        while (true) {
                            byte[] bytes = new byte[10];
                            socket.getInputStream().read(bytes);
                            System.out.println("服务端收到的数据是：" + new String(bytes));
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
